// CourseSelectionSystem.java
import java.util.ArrayList;
import java.util.List;

public class CourseSelectionSystem {
    
    // Course 类
    static class Course {
        private String courseId;
        private String courseName;
        private String location;
        private String time;
        private Teacher teacher;
        private int maxStudents;
        private int currentStudents;

        public Course(String courseId, String courseName, String location, String time, Teacher teacher, int maxStudents) {
            this.courseId = courseId;
            this.courseName = courseName;
            this.location = location;
            this.time = time;
            this.teacher = teacher;
            this.maxStudents = maxStudents;
            this.currentStudents = 0;
        }

        public String getCourseId() { return courseId; }
        public String getCourseName() { return courseName; }
        public String getLocation() { return location; }
        public String getTime() { return time; }
        public Teacher getTeacher() { return teacher; }
        public int getMaxStudents() { return maxStudents; }
        public int getCurrentStudents() { return currentStudents; }
        
        public void setCurrentStudents(int currentStudents) { 
            this.currentStudents = currentStudents; 
        }
        
        public boolean isFull() {
            return currentStudents >= maxStudents;
        }
        
        public void addStudent() {
            if (!isFull()) {
                currentStudents++;
            }
        }
        
        public void removeStudent() {
            if (currentStudents > 0) {
                currentStudents--;
            }
        }
        
        @Override
        public String toString() {
            return String.format("课程编号：%s | 课程名称：%s | 上课地点：%s | 上课时间：%s | 授课教师：%s | 选课人数：%d/%d",
                    courseId, courseName, location, time, teacher.getName(), currentStudents, maxStudents);
        }
    }

    // Teacher 类
    static class Teacher {
        private String teacherId;
        private String name;
        private String gender;
        private String department;
        private Course teachingCourse;

        public Teacher(String teacherId, String name, String gender, String department) {
            this.teacherId = teacherId;
            this.name = name;
            this.gender = gender;
            this.department = department;
        }

        public String getTeacherId() { return teacherId; }
        public String getName() { return name; }
        public String getGender() { return gender; }
        public String getDepartment() { return department; }
        public Course getTeachingCourse() { return teachingCourse; }

        // 教师开课方法
        public Course openCourse(String courseId, String courseName, String location, String time, int maxStudents) {
            this.teachingCourse = new Course(courseId, courseName, location, time, this, maxStudents);
            System.out.println("教师 " + name + " 成功开设课程：" + courseName);
            return teachingCourse;
        }
        
        public void displayTeachingInfo() {
            if (teachingCourse != null) {
                System.out.println("教师信息：" + name + "(" + teacherId + ") - " + department);
                System.out.println("授课信息：" + teachingCourse);
            } else {
                System.out.println("教师 " + name + " 目前没有授课");
            }
        }
    }

    // Student 类
    static class Student {
        private String studentId;
        private String name;
        private String gender;
        private String major;
        private List<Course> selectedCourses;

        public Student(String studentId, String name, String gender, String major) {
            this.studentId = studentId;
            this.name = name;
            this.gender = gender;
            this.major = major;
            this.selectedCourses = new ArrayList<>();
        }

        public String getStudentId() { return studentId; }
        public String getName() { return name; }
        public String getGender() { return gender; }
        public String getMajor() { return major; }
        public List<Course> getSelectedCourses() { return selectedCourses; }

        // 学生选课方法
        public boolean selectCourse(Course course) {
            if (selectedCourses.contains(course)) {
                System.out.println(name + " 已经选择了该课程：" + course.getCourseName());
                return false;
            }
            
            if (course.isFull()) {
                System.out.println("课程 " + course.getCourseName() + " 已满，无法选择");
                return false;
            }
            
            selectedCourses.add(course);
            course.addStudent();
            System.out.println(name + " 成功选择课程：" + course.getCourseName());
            return true;
        }

        // 学生退课方法
        public boolean dropCourse(Course course) {
            if (selectedCourses.remove(course)) {
                course.removeStudent();
                System.out.println(name + " 成功退选课程：" + course.getCourseName());
                return true;
            } else {
                System.out.println(name + " 没有选择该课程：" + course.getCourseName());
                return false;
            }
        }

        // 打印学生课表
        public void printCourseSchedule() {
            System.out.println("\n========== " + name + " 的课表 ==========");
            System.out.println("学号：" + studentId + " | 姓名：" + name + " | 专业：" + major);
            System.out.println("==========================================");
            
            if (selectedCourses.isEmpty()) {
                System.out.println("当前没有选择任何课程");
            } else {
                System.out.printf("%-10s %-15s %-10s %-10s %-10s\n", 
                    "课程编号", "课程名称", "上课地点", "上课时间", "授课教师");
                System.out.println("----------------------------------------------------------");
                
                for (Course course : selectedCourses) {
                    System.out.printf("%-12s %-15s %-12s %-12s %-10s\n",
                        course.getCourseId(),
                        course.getCourseName(),
                        course.getLocation(),
                        course.getTime(),
                        course.getTeacher().getName());
                }
            }
            System.out.println("==========================================\n");
        }
    }

    // 主测试类 - 修复了main方法的声明
    public static void main(String[] args) {
        System.out.println("=== 学生选课模拟系统 ===\n");
        
        // 1. 创建教师
        Teacher teacher1 = new Teacher("T001", "张教授", "男", "计算机科学");
        Teacher teacher2 = new Teacher("T002", "李教授", "女", "数学");
        Teacher teacher3 = new Teacher("T003", "王教授", "男", "物理");
        
        // 2. 教师开课
        System.out.println("=== 教师开课 ===");
        Course mathCourse = teacher1.openCourse("C001", "高等数学", "教学楼A101", "周一 8:00-10:00", 30);
        Course javaCourse = teacher2.openCourse("C002", "Java程序设计", "实验楼B201", "周三 14:00-16:00", 25);
        Course physicsCourse = teacher3.openCourse("C003", "大学物理", "教学楼A205", "周五 10:00-12:00", 35);
        
        System.out.println();
        
        // 3. 创建学生
        Student student1 = new Student("S001", "张三", "男", "计算机科学");
        Student student2 = new Student("S002", "李四", "女", "软件工程");
        Student student3 = new Student("S003", "王五", "男", "信息技术");
        
        // 4. 学生选课
        System.out.println("=== 学生选课 ===");
        student1.selectCourse(mathCourse);
        student1.selectCourse(javaCourse);
        student1.selectCourse(physicsCourse);
        
        student2.selectCourse(mathCourse);
        student2.selectCourse(javaCourse);
        
        student3.selectCourse(mathCourse);
        student3.selectCourse(physicsCourse);
        
        System.out.println();
        
        // 5. 打印学生课表
        System.out.println("=== 打印学生课表 ===");
        student1.printCourseSchedule();
        student2.printCourseSchedule();
        student3.printCourseSchedule();
        
        // 6. 学生退课
        System.out.println("=== 学生退课 ===");
        student1.dropCourse(mathCourse);
        student1.printCourseSchedule();
        
        // 7. 显示教师授课信息
        System.out.println("=== 教师授课信息 ===");
        teacher1.displayTeachingInfo();
        teacher2.displayTeachingInfo();
        teacher3.displayTeachingInfo();
        
        // 8. 测试课程已满情况
        System.out.println("=== 测试课程容量 ===");
        // 创建一个容量小的课程进行测试
        Course smallCourse = teacher1.openCourse("C004", "小班课程", "教学楼A101", "周二 8:00-10:00", 2);
        
        Student testStudent1 = new Student("S004", "测试学生1", "男", "测试专业");
        Student testStudent2 = new Student("S005", "测试学生2", "女", "测试专业");
        Student testStudent3 = new Student("S006", "测试学生3", "男", "测试专业");
        
        testStudent1.selectCourse(smallCourse);
        testStudent2.selectCourse(smallCourse);
        testStudent3.selectCourse(smallCourse); // 这个应该会失败，因为课程已满
        
        System.out.println("\n=== 系统运行结束 ===");
    }
}